//1. 引入express
const { request, response } = require('express');
const express = require('express');

//2.创建应用对象
const app = express();

//3.创建路由规则
//request 是对请求报文的封装
//response 是对响应报文的封装


app.all('/json-server',(request,response)=>{
  //这里/server是请求行的第二个内容url路径
  //如果是/server这时候就会执行这个回调函数里面的代码
  //并且由response来做出响应

    //设置响应头
    response.setHeader('Access-Control-Allow-Origin','*');

    response.setHeader('Access-Control-Allow-Header','*');

    //我们现在在这里想要响应一个数据
    var data = {
        name : 'Joseph and Koseph'
    }

    let str1 = JSON.stringify(data);

    response.send(str1);
});


//配合4-IE文件而创建的规则
app.get('/ie',(request,response)=>{
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Header','*');

    var data2 = {
        name : 'IE'
    }
    let str2 = JSON.stringify(data2);

    response.send(str2);
})

//配合5-超时文件而创建的规则
app.get('/delay',(request,response)=>{
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Header','*');

    setTimeout(()=>{
        response.send("HELLO DELAY");
    },3000)

})

app.get('/sender',(request,response)=>{
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Header','*');

    response.send("已经被发送了");

})

//4.监听端口启动服务
app.listen(7000,()=>{
    console.log("服务已经启动，7000 端口监听中...")
});